#Binary parser template

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class ScionsOfFate_skin(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(ScionsOfFate_skin,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_materials(self, filename):
        
        self.create_material(0)
        self.add_material_name(0, filename)
        self.add_texture_name(0, filename + ".tga")
        
    def parse_vertices(self, meshName, numVerts, vertType):
        
        for i in xrange(numVerts):
            vx, vy, vz = self.inFile.read_float(3)
            self.inFile.read_float(vertType)
            nx, ny, nz, tu, tv = self.inFile.read_float(5)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            self.add_vert_uv(meshName, i, [tu, tv])
            
    def parse_faces(self, meshName, numFaces):
        
        for i in xrange(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            self.add_face_material(meshName, i, 0)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        meshName = "obj"
        self.create_object(meshName)
        
        unk = self.inFile.read_byte()
        numVerts, numFaces, vertType, unk2 = self.inFile.read_long(4)
        self.inFile.read_float(6)
        self.inFile.read_float(16)
        self.inFile.read_float(16)
        
        self.parse_vertices(meshName, numVerts, vertType)
        self.parse_faces(meshName, numFaces)
        
def read_file(path):
    '''Read the file'''
    
    filename, ext = os.path.splitext(path)
    openFile = StructReader(open(path, 'rb'))
    obj = ScionsOfFate_skin(inFile=openFile)
    obj.parse_materials(filename)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "SKIN", "Scions of Fate SKIN"

if __name__ == '__main__':
    
    file1 = ""
    obj = read_file(file1)
    